package com.rao.gmall.product.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author nzmxfan
 * @create 2021-08-05-12:17
 */
@ConfigurationProperties(prefix = "minio")
@Configuration
@Data
public class MinioConfig {

    private String url;
    private String accessKey;
    private String secretKey;
    private String defaultBucket;

    @Bean
    public MinioClient minioClient() {
        try {
            // 创建客户端
            MinioClient minioClient = new MinioClient(
                    url,
                    accessKey,
                    secretKey);
            // 判断是否存在默认 bucket
            boolean exists = minioClient.bucketExists(defaultBucket);
            if (!exists){
                // 不存在则创建 bucket
                minioClient.makeBucket(defaultBucket);
            }
            return minioClient;
        } catch (Exception e) {
            return null;
        }

    }
}
